package UIFree.effects.other {
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class oef_starsLayer extends uif_ecomponent {
		private var _options	: Object;
		private var _timer		: uif_Timer;
		private var tMatrix		: Matrix;
		
		public static const ROUND_TYPE: int = 0;
		public static const STAR_TYPE: int = 1;
		public static const RAYS_TYPE: int = 2;
		
		public function oef_starsLayer(parent:uif_component, rect: Rectangle=null, a_options: Object=null) {
			rect = rect?rect:parent.getInner();
			super(parent, rect.x, rect.y, rect.width, rect.height);
			_options = uif_util.union({
				alphaSpeed	: 0.05,
				tr			: [1,0,0,1,0,0],
				minRadius	: 1,
				maxRadius	: 5,
				density		: 0.2,
				delay		: 50,
				repeat		: 0,
				color		: 0xFFFFFF,
				type		: ROUND_TYPE,
				
				rays		: 5,
				spot		: 0.5	
			}, a_options);
			
			tMatrix = new Matrix(_options.tr[0],_options.tr[1],_options.tr[2],_options.tr[3],_options.tr[4],_options.tr[5]);
			_timer = new uif_Timer(_options.delay, doStartCreate, null, _options.repeat);
			_timer.start();
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(Event.ENTER_FRAME, doEnterFrame);
		}
		
		protected function doEnterFrame(e: Event): void {
			forEach(function(star: Shape): void {
				if (star.alpha < _options.alphaSpeed) {
					removeChild(star);
					if (numChildren == 0) dispatchEvent(new Event(Event.COMPLETE));
				}
				star.alpha -= _options.alphaSpeed;
				var mat: Matrix = tMatrix.clone();
				mat.concat(star.transform.matrix);
				star.transform.matrix = mat;
			});
		}
		
		protected function doStartCreate(e: Event): void {
			if (Math.random() > 1 - _options.density) addStar();
		}
		
		protected function newStar(): Shape {
			var star	: Shape 	= new Shape();
			switch (_options.type) {
				case ROUND_TYPE: asRoundType(star.graphics, rndRadius);
					break;
				case STAR_TYPE: asStarType(star.graphics, rndRadius);
					break;
				case RAYS_TYPE: asRaysType(star.graphics, rndRadius);
					break;
			}
			return star;
		}
		
		protected function get rndRadius(): Number {
			return _options.minRadius + Math.random() * (_options.maxRadius - _options.minRadius);
		}
		
		protected function asRoundType(graphics: Graphics, radius: Number): void {
			var mat		: Matrix 	= new Matrix();
			mat.createGradientBox(radius * 2, radius * 2, 0, -radius, -radius); 
			graphics.beginGradientFill(GradientType.RADIAL, [_options.color, _options.color], [1, 0], [0x88, 0xFF], mat);
			//star.graphics.beginFill(0xFFFFFF, 1);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
		
		protected function asStarType(graphics: Graphics, radius: Number): void {
			var mat		: Matrix 	= new Matrix();
			graphics.beginFill(_options.color);
			graphics.lineStyle(1, _options.color);
			uif_util.drawStar(graphics, radius, radius * _options.spot, _options.rays);
			graphics.endFill();
		}
		
		protected function asRaysType(graphics: Graphics, radius: Number): void {
			asRoundType(graphics, radius * 0.2);
			uif_util.drawFillRays(graphics, radius, 6, _options.color);
		}
		
		protected function addStar(): void {
			var star	: Shape = newStar();
			
			while (true) {
				var p: Point = new Point(Math.random() * width, Math.random() * height);
				if (!_options.mask || (_options.mask as BitmapData).getPixel(p.x, p.y)) {
					star.x = p.x;
					star.y = p.y;
					break;
				}
			}
			
			addChild(star);
		}
		
		override public function dispose():void {
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, doStartCreate);
			super.dispose();
		}
	}
}